package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjNCementMatchBo;
import com.ruoyi.zhgd.domain.vo.ZjNCementMatchVo;
import com.ruoyi.zhgd.domain.ZjNCementMatch;
import com.ruoyi.zhgd.mapper.ZjNCementMatchMapper;
import com.ruoyi.zhgd.service.IZjNCementMatchService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自建-水稳配比信息Service业务层处理
 *
 * @author tll
 * @date 2024-10-08
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjNCementMatchServiceImpl implements IZjNCementMatchService {

    private final ZjNCementMatchMapper baseMapper;

    /**
     * 查询自建-水稳配比信息
     */
    @Override
    public ZjNCementMatchVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自建-水稳配比信息列表
     */
    @Override
    public TableDataInfo<ZjNCementMatchVo> queryPageList(ZjNCementMatchBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<ZjNCementMatch> lqw = buildQueryWrapper(bo);
//        Page<ZjNCementMatchVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<ZjNCementMatchVo> result = baseMapper.page(bo, pageQuery.build());
        return TableDataInfo.build(result);
    }

    /**
     * 查询自建-水稳配比信息列表
     */
    @Override
    public List<ZjNCementMatchVo> queryList(ZjNCementMatchBo bo) {
        LambdaQueryWrapper<ZjNCementMatch> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjNCementMatch> buildQueryWrapper(ZjNCementMatchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjNCementMatch> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMatchName()), ZjNCementMatch::getMatchName, bo.getMatchName());
        lqw.eq(bo.getCreator() != null, ZjNCementMatch::getCreator, bo.getCreator());
        lqw.eq(bo.getCreateDate() != null, ZjNCementMatch::getCreateDate, bo.getCreateDate());
        lqw.eq(bo.getUpdater() != null, ZjNCementMatch::getUpdater, bo.getUpdater());
        lqw.eq(bo.getUpdateDate() != null, ZjNCementMatch::getUpdateDate, bo.getUpdateDate());
        lqw.eq(bo.getGravel1Min() != null, ZjNCementMatch::getGravel1Min, bo.getGravel1Min());
        lqw.eq(bo.getGravel1Max() != null, ZjNCementMatch::getGravel1Max, bo.getGravel1Max());
        lqw.eq(bo.getGravel2Min() != null, ZjNCementMatch::getGravel2Min, bo.getGravel2Min());
        lqw.eq(bo.getGravel2Max() != null, ZjNCementMatch::getGravel2Max, bo.getGravel2Max());
        lqw.eq(bo.getGravel3Min() != null, ZjNCementMatch::getGravel3Min, bo.getGravel3Min());
        lqw.eq(bo.getGravel3Max() != null, ZjNCementMatch::getGravel3Max, bo.getGravel3Max());
        lqw.eq(bo.getGravel4Min() != null, ZjNCementMatch::getGravel4Min, bo.getGravel4Min());
        lqw.eq(bo.getGravel4Max() != null, ZjNCementMatch::getGravel4Max, bo.getGravel4Max());
        lqw.eq(bo.getGravel5Min() != null, ZjNCementMatch::getGravel5Min, bo.getGravel5Min());
        lqw.eq(bo.getGravel5Max() != null, ZjNCementMatch::getGravel5Max, bo.getGravel5Max());
        lqw.eq(bo.getGravel6Min() != null, ZjNCementMatch::getGravel6Min, bo.getGravel6Min());
        lqw.eq(bo.getGravel6Max() != null, ZjNCementMatch::getGravel6Max, bo.getGravel6Max());
        lqw.eq(bo.getPowder1Min() != null, ZjNCementMatch::getPowder1Min, bo.getPowder1Min());
        lqw.eq(bo.getPowder1Max() != null, ZjNCementMatch::getPowder1Max, bo.getPowder1Max());
        lqw.eq(bo.getPowder2Min() != null, ZjNCementMatch::getPowder2Min, bo.getPowder2Min());
        lqw.eq(bo.getPowder2Max() != null, ZjNCementMatch::getPowder2Max, bo.getPowder2Max());
        lqw.eq(bo.getWaterMin() != null, ZjNCementMatch::getWaterMin, bo.getWaterMin());
        lqw.eq(bo.getWaterMax() != null, ZjNCementMatch::getWaterMax, bo.getWaterMax());
        lqw.eq(bo.getSjbMin() != null, ZjNCementMatch::getSjbMin, bo.getSjbMin());
        lqw.eq(bo.getSjbMax() != null, ZjNCementMatch::getSjbMax, bo.getSjbMax());
        lqw.eq(StringUtils.isNotBlank(bo.getStationCode()), ZjNCementMatch::getStationCode, bo.getStationCode());
        lqw.like(StringUtils.isNotBlank(bo.getStationName()), ZjNCementMatch::getStationName, bo.getStationName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ZjNCementMatch::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增自建-水稳配比信息
     */
    @Override
    public Boolean insertByBo(ZjNCementMatchBo bo) {
        ZjNCementMatch add = BeanUtil.toBean(bo, ZjNCementMatch.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自建-水稳配比信息
     */
    @Override
    public Boolean updateByBo(ZjNCementMatchBo bo) {
        ZjNCementMatch update = BeanUtil.toBean(bo, ZjNCementMatch.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjNCementMatch entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自建-水稳配比信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取该标段的数据启用信息
     *
     * @param id
     * @return
     */
    @Override
    public Boolean getEnabledInfo(String id) {
        int count = baseMapper.getEnabledInfo(id);
        return count > 0;
    }
}
